<template>
  <el-card>
    <el-button
      @click="$router.push('/category/add')"
      type="primary"
      icon="el-icon-circle-plus-outline"
    >添加</el-button>
    <el-divider />
    <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID" sortable width="180"></el-table-column>
      <el-table-column prop="catename" label="分类名称" sortable width="180"></el-table-column>
      <el-table-column prop="img" label="分类图片">
        <template v-slot="scope">
          <el-image
            :style="{width:'100px'}"
            v-if="scope.row.img!=''"
            :src="$imgBaseUrl+scope.row.img"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status==1">正常</el-tag>
          <el-tag type="error" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            @click="edit(scope.row.id)"
            type="primary"
            size="mini"
            circle
            icon="el-icon-edit"
          ></el-button>
          <el-button
            @click="del(scope.row.id)"
            type="danger"
            size="mini"
            circle
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      list: []
    };
  },
  methods: {
    // 请求商品分类信息
    async getCates() {
      const res = await this.$http.get("/api/catelist?istree=1");
      if (res.code == 200) {
        this.list = res.list;
      }
    },
    edit(id) {
      // console.log(id);
      this.$router.push(`/category/${id}`);
    },
    del(id) {
      this.$myConfirm(async ()=>{
         const res=await this.$http.post('/api/catedelete',{id});
         if(res.code==200){
          //  移除页面中被删除的数据
          // 1-获取被删除元素的下标
          const  index=this.list.findIndex(item=>item.id==id);
          // 2-通过split移除数据元素
          this.list.splice(index,1);

         }
      });
    }
  },
  created() {
    this.getCates();
  }
};
</script>

<style>
</style>